﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Business
{
    public partial class ProcessCount : BasePage//System.Web.UI.Page
    {
        //ckBind判断ddl 的数据是否绑定
        private static bool ckBind;

        private Lc_ProcessBLL bll = new Lc_ProcessBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "cmid";
                ViewState["OrderDire"] = "asc";

                txt_time_begin.Value = DateTime.Today.Year.ToString() + "-01-01";
                txt_time_end.Value = DateTime.Now.ToString("yyyy-MM-dd");
                ckBind = false;

                BindData(true);
                //bindData(txt_time_begin.Value, txt_time_end.Value, "");
            }
        }

        //private void findWhere()
        //{
        //    string begin = txt_time_begin.Value;
        //    string end = txt_time_end.Value;
        //    string id = this.hidCompanyId.Value.ToString();

        //    if (begin == "")
        //    {
        //        begin = "1900-02-02";
        //    }
        //    if (end == "")
        //    {
        //        end = DateTime.Now.ToString("yyyy-MM-dd");
        //    }
        //    bindData(begin, end, id);
        //}
        //private void bindData(string begin, string end, string comId)
        //{
        //    if (!ckBind)
        //    {
        //        ckBind = true;
        //    }
        //    // DateTime.Now.ToString("yyyy-MM-dd")
        //    DataTable dts = new Qs_ApplyDetailBLL().getProcessCount(begin, end, comId);
        //    ViewState["database"] = dts;
        //    grvGoodsShows.DataSource = dts;
        //    grvGoodsShows.DataBind();

        //}
        //protected void pagings_DataBinding(object sender, EventArgs e)
        //{
        //    bindData("1900-02-02", DateTime.Now.ToString("yyyy-MM-dd"), "");
        //}

        /// <summary>
        /// 点击查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //findWhere();
            BindData(false);
        }

        protected void grvGoodsShows_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grvGoodsShows.PageIndex = e.NewPageIndex;

                //if (this.hidCompanyId.Value != "")
                //{
                //findWhere();
                BindData(false);
                //}
                //else
                //{
                //bindData("1900-01-05", DateTime.Now.ToString("yyyy-MM-dd"), "");
                //}

                TextBox tb = (TextBox)grvGoodsShows.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (grvGoodsShows.PageIndex + 1).ToString();
            }
            catch { }
        }

        protected void grvGoodsShows_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "go")
                {
                    TextBox tb = (TextBox)this.grvGoodsShows.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    grvGoodsShows_PageIndexChanging(null, ea);
                }
            }
            catch (Exception)
            {
            }
        }

        #region 导出

        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            string fileName = "流程统计报表_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string str = "导出人：" + (HttpContext.Current.Session["SysUserInfo"] as Sys_User).USER_NAME + "&nbsp;&nbsp;&nbsp;导出时间：" + DateTime.Now;
            lblTJ.Text = str;
            ExportExcel(pnl, fileName);
        }

        /// <summary>
        /// 导出方法
        /// </summary>
        /// <param name="objControl"></param>
        /// <param name="strFileName"></param>
        public void ExportExcel(System.Web.UI.WebControls.WebControl objControl, string strFileName)
        {
            DataTable dt = (DataTable)ViewState["database"];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "R")
                {
                    dt.Columns[i].ColumnName = "序号";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "zhuxiao")
                {
                    dt.Columns[i].ColumnName = "注销";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "zhengzaibanli")
                {
                    dt.Columns[i].ColumnName = "正在办理";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "guidang")
                {
                    dt.Columns[i].ColumnName = "归档";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "names")
                {
                    dt.Columns[i].ColumnName = "流程名称";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "cinames")
                {
                    dt.Columns[i].ColumnName = "公司名称";
                }
                if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "cmid" || dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "idss")
                {
                    dt.Columns.Remove(dt.Columns[i].ColumnName);
                    if (i == 0)
                    {
                        i = 0;
                    }
                    else
                    {
                        i--;
                    }
                }
            }
            toExecl1(dt, "流程统计" + DateTime.Now.ToString());
        }

        /*{
            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        */

        #endregion 导出

        #region 打印

        protected void BtnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                var key = Guid.NewGuid().ToString();
                Session[key + "Header"] = "流程统计";

                DataTable dt = (DataTable)ViewState["database"];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "r")
                    {
                        dt.Columns[i].ColumnName = "序号";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "zhuxiao")
                    {
                        dt.Columns[i].ColumnName = "注销";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "zhengzaibanli")
                    {
                        dt.Columns[i].ColumnName = "正在办理";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "guidang")
                    {
                        dt.Columns[i].ColumnName = "归档";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "names")
                    {
                        dt.Columns[i].ColumnName = "流程名称";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "cinames")
                    {
                        dt.Columns[i].ColumnName = "公司名称";
                    }
                    if (dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "cmid" || dt.Columns[i].ColumnName.Trim().ToString().ToLowerInvariant() == "idss")
                    {
                        dt.Columns.Remove(dt.Columns[i].ColumnName);
                        if (i == 0)
                        {
                            i = 0;
                        }
                        else
                        {
                            i--;
                        }
                    }
                }
                Session[key + "Body"] = dt;

                MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=BASIC&VALUE=" + key + "\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
            }
            catch (Exception)
            {
                MessageBox.Show(Page, "请先点击查询！！");
            }
        }

        #endregion 打印

        /// <summary>
        /// GridView标题列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvGoodsShows_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            //BindProjectName(true);
            BindData(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        //public void BindProjectName(bool iSsort)
        //{
        //    string begin = txt_time_begin.Value;
        //    string end = txt_time_end.Value;
        //    string id = this.hidCompanyId.Value.ToString();

        //    if (begin == "")
        //    {
        //        begin = "1900-02-02";
        //    }
        //    if (end == "")
        //    {
        //        end = DateTime.Now.ToString("yyyy-MM-dd");
        //    }

        //    //lbltitle.InnerHtml = Convert.ToDateTime(begin).ToString("yyyy.MM.dd") + "至" + Convert.ToDateTime(end).ToString("yyyy.MM.dd") + "流程统计";

        //    DataTable dts = new Qs_ApplyDetailBLL().getProcessCount(begin, end, id);
        //    ViewState["database"] = dts;
        //    grvGoodsShows.DataSource = dts;
        //    grvGoodsShows.DataBind();

        //    DataView view = dts.DefaultView;
        //    if (iSsort)
        //    {
        //        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        //        view.Sort = sort;//设置排序
        //    }

        //    grvGoodsShows.DataSource = view;

        //    grvGoodsShows.DataBind();
        //}

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        private void BindData(bool isFirst)
        {
            string begin_time = txt_time_begin.Value;
            string end_time = txt_time_end.Value;
            string com_name = "";

            if (FORM_COMPANY_ID.Value != -1)
            {
                com_name = FORM_COMPANY_ID.Value.ToString();
            }
            else
            {
                com_name = @"select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid";
            }
            if (begin_time == "")
            {
                begin_time = "1900-02-02";
            }
            if (end_time == "")
            {
                end_time = DateTime.Now.ToString("yyyy-MM-dd");
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = bll.GetRecordCount1(com_name, begin_time, end_time);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bll.GetListByPage1(com_name, begin_time, end_time, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvGoodsShows;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvGoodsShows;
                grvGoodsShows.DataSource = null;
                grvGoodsShows.DataBind();
                PageIng.Visible = false;
            }
        }
    }
}